package com.asia5b.a5mall.middleware.utils

import android.graphics.Typeface
import androidx.core.content.ContextCompat
import android.text.Spannable
import android.text.SpannableString
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.widget.TextView
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.BaseApplication
import com.asia5b.a5mall.middleware.config.CountrysRepository

/**
 * Created by zhangyuncai on 2020/5/8.
 */
class CurrencyPriceBuilder3 {

    /**
     * @param bold 字体加粗
     * @param color 字体颜色
     * @param size 字体大小
     */
    data class AttrBean(var bold: Boolean = false, var color: Int = R.color.common_text_black, var size: Int = 14)

    //目标TextView
    var textView: TextView? = null

    //目标价格
    var price = -1.0

    //目标价格符号 ¥或RMB
    var currencyMark = CountrysRepository.getCurrenCurrencyMark()
    var currencyShort = CountrysRepository.getCurrencySelect().currency_short

    //目标价格为空时,从价格列表寻找当前货币中的价格
    var priceList: HashMap<String, Double>? = null

    //价格符号
    var markAttrBean: CurrencyPriceBuilder3.AttrBean? = null

    //数字和pointAfterAttrBean互斥
    var numberAttrBean: CurrencyPriceBuilder3.AttrBean? = null

    //数字小数点前(不包括小数点)和pointBeforeAttrBean配对
    var pointAfterAttrBean: CurrencyPriceBuilder3.AttrBean? = null

    //数字小数点后(包括小数点)
    var pointBeforeAttrBean: CurrencyPriceBuilder3.AttrBean? = null

    /**
     * 返回不带格式的字符串
     */
    fun buildPriceText(): String {
        whenPriceNull()
        val priceString = String.format("%.2f", price)
        val priceCombin = String.format("%s %s", currencyMark, priceString)
        return priceCombin
    }

    /**
     * 常用配置
     */
    fun build14_18() {//价格显示红色
        markAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.color_red, 14)
        pointAfterAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.color_red, 18)
        pointBeforeAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.color_red, 14)
        build()
    }

    fun buildBlue14_18() {
        markAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.common_text_blue, 14)
        pointAfterAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.common_text_blue, 18)
        pointBeforeAttrBean = CurrencyPriceBuilder3.AttrBean(true, R.color.common_text_blue, 14)
        build()
    }

    /**
     * 常用配置
     */
    fun build14_18_2() {
//        markAttrBean= CurrencyPriceBuilder3.AttrBean(true,R.color.color_red,14)
//        pointAfterAttrBean=CurrencyPriceBuilder3.AttrBean(true,R.color.color_red,18)
//        pointBeforeAttrBean=CurrencyPriceBuilder3.AttrBean(true,R.color.color_red,14)
//        build()
        build14_18()
    }

    fun build(): SpannableString {
        whenPriceNull()

        val priceString = String.format("%.2f", price)
        val priceCombin = String.format("%s %s", currencyMark, priceString)
        var spannableString = SpannableString(priceCombin)

        var markIndexStart = 0
        var markIndexEnd = currencyMark.length

        var numIndexStart = markIndexEnd + 1
        var numIndexEnd = priceCombin.length

        var pointAfterStart = markIndexEnd + 1
        var pointAfterEnd = priceCombin.indexOf(".")

        var pointBeforeStart = pointAfterEnd + 1
        var pointBeforeEnd = priceCombin.length

        var setSpan = { attrBean: AttrBean, indexStart: Int, indexEnd: Int ->
            attrBean.run {
                //加粗
                if (this.bold) {
                    spannableString.setSpan(StyleSpan(Typeface.BOLD), indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                }
                //颜色
                var colorSpan = ForegroundColorSpan(ContextCompat.getColor(BaseApplication.sContext, this.color))
                spannableString.setSpan(colorSpan, indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
                //大小
                var sizeSpan = AbsoluteSizeSpan(DisplayUtils.dip2px(BaseApplication.sContext, this.size.toFloat()))
                spannableString.setSpan(sizeSpan, indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
            }
        }

        markAttrBean?.apply {
            setSpan(this, markIndexStart, markIndexEnd)
        }

        if (numberAttrBean != null) {
            setSpan(numberAttrBean!!, numIndexStart, numIndexEnd)
        } else {
            if (pointAfterAttrBean != null && pointBeforeAttrBean != null) {
                setSpan(pointAfterAttrBean!!, pointAfterStart, pointAfterEnd)
                setSpan(pointBeforeAttrBean!!, pointBeforeStart, pointBeforeEnd)
            }
        }

        //设置进控件
        textView?.run {
            text = spannableString
        }

        dddLog("priceCombin:$priceCombin")
        return spannableString
    }

    fun whenPriceNull() {
        if (price < 0) {//价格
            priceList?.forEach { entry ->
                if (entry.key.toLowerCase().contains(currencyShort.toLowerCase())) {
                    price = entry.value
                }
            }
        }
        if (price < 0) {//价格再次判断
            price = 0.0
        }

    }
}